package com.liukai.user.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.liukai.novel_base.base.BaseFragment;
import com.liukai.novel_commons.db.DBHelper;
import com.liukai.novel_commons.db.UserInfoService;
import com.liukai.novel_commons.entities.NovelRack;
import com.liukai.novel_commons.entities.UserInfo;
import com.liukai.novel_commons.utils.Constans;
import com.liukai.novel_commons.utils.LogUtils;
import com.liukai.novel_commons.utils.MyInfoList;
import com.liukai.novel_commons.utils.State;
import com.liukai.user.R;
import com.liukai.user.R2;
import com.liukai.user.activity.LoginActivity;
import com.liukai.user.activity.SettingActivity;
import com.liukai.user.adapter.MyInfoRecyclerAdapter;

import java.io.Serializable;
import java.util.ArrayList;

import butterknife.BindView;

public class MyInfoFragment extends BaseFragment {

    private Context mContext;

    @BindView(R2.id.recyclerView)
    RecyclerView mRecyclerView;

    @BindView(R2.id.btn_login)
    TextView btnLogin;

    @BindView(R2.id.tv_user_name)
    TextView tvUserName;

    private MyInfoRecyclerAdapter mMyInfoRecyclerAdapter;
    private UserInfo mUserInfo;

    /**
     * 初始化数据
     */
    @Override
    protected void initDate(View view) {
        super.initDate(view);
        setUpState(State.SUCCESS);
    }

    /**
     * 获取Fragment的上下文
     *
     * @param view
     * @return
     */
    @Override
    protected Context getFragmentContext(View view) {
        this.mContext = view.getContext();
        return null;
    }


    /**
     * 绑定组件
     *
     * @param view
     */
    @RequiresApi(api = Build.VERSION_CODES.P)
    @Override
    protected void initView(View view) {
        super.initView(view);
        mUserInfo = Constans.getUserInfo();
        if (mUserInfo.getUserId() == null || mUserInfo.getUserId().equals("")) {
            return;
        }
        btnLogin.setVisibility(View.GONE);
        tvUserName.setVisibility(View.VISIBLE);
        tvUserName.setText(mUserInfo.getUserName());



    }

    /**
     * 设置适配器
     */
    @Override
    protected void initAdapter(View view) {
        mMyInfoRecyclerAdapter = new MyInfoRecyclerAdapter(MyInfoList.getMyInfoList());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(view.getContext());
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.setAdapter(mMyInfoRecyclerAdapter);
    }

    /**
     * 设置监听
     */
    @Override
    protected void initListener() {

        // item点击事件监听
        mMyInfoRecyclerAdapter.setItemClick(new MyInfoRecyclerAdapter.ItemClick() {
            Intent mIntent;

            @Override
            public void onItemClickListener(String itemName) {
                LogUtils.d(getClass(), itemName);
                switch (itemName) {
                    case "设置":
                        mIntent = new Intent(mContext, SettingActivity.class);
                        startActivityForResult(new Intent(mContext, SettingActivity.class), Constans.CODE_REQUEST_MyInfoFragment);
                        break;
                }

            }
        });

        // 点击登录
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtils.d(getClass(), "登录");
                startActivityForResult(new Intent(mContext, LoginActivity.class), Constans.CODE_REQUEST_MyInfoFragment);
            }
        });
    }

    /**
     * 获取布局的ID
     *
     * @return
     */
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_my_info;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        LogUtils.e(getClass(), "resultCode" + resultCode);
        LogUtils.e(getClass(), "requestCode" + requestCode);
        // 获取登录界面返回的数据
        if (requestCode == Constans.CODE_REQUEST_MyInfoFragment && resultCode == Constans.CODE_RESULT_LoginActivity) {
            UserInfo userInfo = (UserInfo) data.getSerializableExtra("userInfo");
            LogUtils.e(getClass(), "userInfo" + userInfo.toString());
            // 设置用户名
            tvUserName.setVisibility(View.VISIBLE);
            tvUserName.setText(userInfo.getUserName());
            btnLogin.setVisibility(View.GONE);
        }
        // 获取设置界面返回的数据
        if (requestCode == Constans.CODE_REQUEST_MyInfoFragment && resultCode == Constans.CODE_RESULT_SettingActivity) {
            // 清空本地保存的用户信息
            Constans.setUserInfo(new UserInfo());
            Constans.setNovelRackList(new ArrayList<NovelRack>());
            MyInfoList.removeSettingList(Constans.LOGOUT);
            tvUserName.setVisibility(View.GONE);
            btnLogin.setVisibility(View.VISIBLE);
        }
    }
}
